<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\controller\auth\Admin;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use fast\Random;
use fast\Tree;
use think\Validate;

/**
 * 企业信息管理
 *
 * @icon fa fa-circle-o
 */
class Merchant extends Backend
{
    
    /**
     * Merchant模型对象
     * @var \app\admin\model\Merchant
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Merchant;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 添加企业 并且 生成默认企业管理员
     */
    public function addMerchant()
    {
        if ($this->request->isAjax()) {
            // $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                // 添加企业 --merchant表
                $this->model->startTrans();
                $result = $this->model->validate()->save($params);
                if ($result === false) {
                    $this->model->rollback();
                    $this->error($this->model->getError());
                }

                // 添加企业对应管理员 --admin表
                $merchant_name = $params["merchant_name"];
                $merchant = $this->model->where('merchant_name',$merchant_name)->find();

                $adminParams["merchant_id"] = $merchant->merchant_id;
                $adminParams["username"] = $merchant->merchant_tel;
                $adminParams["nickname"] = $merchant->abbr;
                $adminParams["telephone"] = $merchant->merchant_tel;
                $adminParams['salt'] = Random::alnum();
                $adminParams["password"] = md5(md5($merchant->merchant_tel) . $adminParams['salt']);
                $adminParams['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。

                $adminModel = new \app\admin\model\Admin;
                $adminModel->startTrans();
                $result = $adminModel->validate('Admin.add')->save($adminParams);
                if ($result === false) {
                    $adminModel->rollback();
                    $this->model->rollback();
                    $this->error($adminModel->getError());
                }

                // 设置权限 --AuthGroupAccess表 
                $group = AuthGroup::get(["name" =>"上杭企业"]);
                $admin = $adminModel->where("username",$merchant->merchant_tel)->find();
                $groupAccessParams["uid"] = $admin->id;   // 新增的管理员ID
                $groupAccessParams["group_id"] = $group->id; // 企业权限组对应的 组ID

                $groupAccessModel = new \app\admin\model\AuthGroupAccess();
                $groupAccessModel->startTrans();
                $result = $groupAccessModel->save($groupAccessParams);
                if ($result === false) {
                    $groupAccessModel->rollback();
                    $adminModel->rollback();
                    $this->model->rollback();
                    $this->error($groupAccessModel->getError());
                }

                $groupAccessModel->commit();
                $adminModel->commit();
                $this->model->commit();

                // alert_success("添加成功\n该企业后台管理员账号：".$merchant->merchant_tel."\n初始密码：".$merchant->merchant_tel);
                $this->success($data=$adminParams);
            }
            $this->error();
        }
        return $this->view->fetch();
    }
}
